package com.jgy.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author :yangz
 * @date : 2023/5/4
 */
public class TimeUtil {

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String HH_MM_SS = "HH:mm:ss";
    public static final String YYYY_MM_DD_HH = "yyyy-MM-dd HH";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String HH_MM= "HH:mm";
    public static final String HH= "HH";
    public static final String DD= "dd";
    public static final String MM= "mm";
    public static final String YYYY_MM= "yyyy-MM";
    public static final String YYYY= "yyyy";

    /**
     * 获取两个时间段内的每一天
     *
     * @param start
     * @param end
     * @return
     */
    public static  List<String> getBetweenDayDate(LocalDate start, LocalDate end) {

        List<String> list = new ArrayList<>();
        long distance = ChronoUnit.DAYS.between(start, end);
        Stream.iterate(start, date -> date.plusDays(1)).

                limit(distance + 1).
                forEach(day -> list.add(day.format(DateTimeFormatter.ofPattern(YYYY_MM_DD))));
        return list;
    }
    public static  List<String> getBetweenTimeDate(String start, String end,String format) {

        LocalDateTime startTime = LocalDateTime.parse(start , DateTimeFormatter.ofPattern(format));
        LocalDateTime endTime = LocalDateTime.parse(end , DateTimeFormatter.ofPattern(format));
        List<String> list = new ArrayList<>();
        LocalDateTime current = startTime;
        while (current.isBefore(endTime)) {
            list.add(current.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            current = current.plusMinutes(1);
        }
        list.add(endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return list;

    }

    public static  List<String> getBetweenDayDate(String startStr, String endStr) {
        LocalDate start=LocalDate.parse(startStr,DateTimeFormatter.ofPattern(YYYY_MM_DD));
        LocalDate end=LocalDate.parse(endStr,DateTimeFormatter.ofPattern(YYYY_MM_DD));

        List<String> list = new ArrayList<>();
        long distance = ChronoUnit.DAYS.between(start, end);
        Stream.iterate(start, date -> date.plusDays(1)).

                limit(distance + 1).

                forEach(day -> list.add(day.format(DateTimeFormatter.ofPattern(YYYY_MM_DD))));
        return list;

    }

    public static List<String> getBetweenMonthDate(String startStr, String endStr) {
        LocalDate start = LocalDate.parse(startStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate end = LocalDate.parse(endStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        List<String> list = new ArrayList<>();
        long distance = ChronoUnit.MONTHS.between(
                YearMonth.from(start.withDayOfMonth(1)),
                YearMonth.from(end.withDayOfMonth(1))
        );
        Stream.iterate(start.withDayOfMonth(1), date -> date.plusMonths(1))
                .limit(distance + 1)
                .forEach(month -> list.add(month.format(DateTimeFormatter.ofPattern("yyyy-MM"))));
        return list;
    }
    public static List<String> getDayListInMonth(String yearMonthStr) {
        ArrayList<String> dayList = new ArrayList<>();

        //解析年月
        YearMonth yearMonth = YearMonth.parse(yearMonthStr);

        //获取这个月的1号
        LocalDate firstDay = yearMonth.atDay(1);

        //遍历这个月的每一天
        int length = yearMonth.lengthOfMonth();
        for (int i = 1; i <= length; i++) {
            LocalDate date = firstDay.plusDays(i - 1);
            dayList.add(date.toString());
        }

        return dayList;
    }

    public static List<String> getMonthListInYear(String yearStr) {
        List<String> monthList = new ArrayList<>();

        // 遍历1-12月
        for (int month = 1; month <= 12; month++) {
            // 构造指定年月的YearMonth对象
            YearMonth yearMonth = YearMonth.of(Integer.parseInt(yearStr), month);
            // 格式化为字符串并添加到列表
            monthList.add(yearMonth.toString());
        }

        return monthList;
    }
}
